﻿USE [BoxSport]

CREATE PROCEDURE [dbo].[ActualizarStock2]

	@pedidoId int
AS
BEGIN
		declare
		@IdProducto int,
		@Cantidad decimal(9,2),
		@newStock decimal(9,2)
	
	declare DisminuirStock Cursor For
			
			select 
				IdProducto,
				Cantidad
			from dbo.DETALLEVENTA
			where IdVenta = @pedidoId
			
		open DisminuirStock
		
			fetch next from DisminuirStock
			
			into @IdProducto,@Cantidad
				
				while @@FETCH_STATUS = 0
				
				Begin
				
				select @newStock = Cantidad from dbo.DETALLEVENTA where IdProducto = @IdProducto
				
				update Producto set Stock = Stock - @newStock
						where id = @IdProducto
						
						fetch next from DisminuirStock
						into @IdProducto,@Cantidad
				End		
		
		close DisminuirStock
		deallocate DisminuirStock

END
